<template>
  <div class="login-container">
      <!-- 导航栏 -->
      <van-nav-bar  title="登录" >
        <van-icon slot="left" name="cross"  @click="$router.back()"/>
      </van-nav-bar>
      <!-- 登陆表单 -->
      <van-form ref="loginForm" @submit="onSubmit">
        <van-field
          v-model="user.mobile"

          type="number"
          name="mobile"
          label="手机号"
          placeholder="请输入手机号"
          :rules="userFormRules.mobile"
          maxlength="11"
        />
        <van-field
        v-model="user.code"
          name="code"
          label="验证码"
          placeholder="请输入验证码"
          :rules="userFormRules.code"
          type="number"
          maxlength="6"
        >
        <template #button>
          <!-- 倒计时
          time就是倒计时时间
          -->
          <van-count-down v-if="isCountDownShow" :time="1000 * 60" format="ss s后可重新发送" @finish="isCountDownShow=false" />
          <van-button round native-type="button" class="send-sms-btn" size="small" type="default" @click="onSendSms" v-else>发送验证码</van-button>
        </template>
      </van-field>
        <div class="login-btn-wrap">
          <van-button class="login-btn"  block type="info" native-type="submit">登录</van-button>
        </div>
      </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'LoginIndex',
  data () {
    return {
      user: {
        mobile: '13911111111', // 手机号
        code: '246810' // 验证码
      },
      userFormRules: {
        mobile: [{ required: true, message: '手机号不能为空' }, {
          // 增加规则
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        code: [{ required: true, message: '验证码不能为空' },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误'
          }
        ]
      },
      isCountDownShow: false
    }
  },
  methods: {
    async onSubmit () {
      // 1.获取表单数据
      const user = this.user
      // 2.表单验证
      // 在组件中必须通过this.$toast来调用toast
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true, // 禁用背景点击
        duration: 0 // 如果为0，则持续展示
        // loadingType: 'spinner'
      })
      // 3.提交表单请求登录
      try {
        const { data } = await login(user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        // 登陆成功，跳转回原来页面
        this.$router.back()
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast.fail('手机或者验证码输入错误')
        } else {
          this.$toast.fail('登陆失败,请稍后重试')
        }
      }
      // 4.根据响应结果处理后续操作
    },
    async onSendSms () {
      console.log('onSendSms')
      // 1.校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log('验证通过')
      } catch (error) {
        return console.log('验证失败', error)
      }
      // 2.验证通过，显示倒计时
      this.isCountDownShow = true
      // 3.请求发送验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (error) {
        // 发送失败关闭倒计时
        this.isCountDownShow = false
        console.log('发送失败', error)
        if (error.response.status === 429) {
          this.$toast('发送太频繁了，请稍后再试')
        } else {
          this.$toast('发送失败')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #3926fa;
  .van-nav-bar__title, .van-icon {
    color: #fff;
  }
}
.send-sms-btn {
  background-color: #ededed;

}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }

}
</style>
